<?php 
include_once __DIR__.'/../vendor/autoload.php';  
if (strpos(strtolower(PHP_OS), 'win') === 0) {
    class_alias(\Windows\Events\Select::class, \Workerman\Events\Select::class);
} 

$e = \Workerman\Worker::$globalEvent = new \Workerman\Events\Select ;
$defer=new class{
    function __destruct(){
        \Workerman\Worker::$globalEvent->loop();
    }
};  
class_exists(Actor::class);
 
 
// Proc::$debug=true;
// Booru::$reg_addr="127.0.0.1:55002";
Booru::startServer(); 

Actor::spawn_opt(function(){ 
    $i=0;
    while(1){ 
        echo "\r:$i ".date('H:i:s')."\n";
        Actor::spawn(function()use($i){ 
            //for ($i=0; $i < 1000; $i++) sqrt(999);

            sleep(1);
            echo "{{$i}  {$this->coid}  ".Booru::$client_addr."}\n"; 
        });
        yield $this->delay(0.1); 
        $i++;
    }
});


// Sup::start(function(){
//     $this->strategy = 'OneForOne';
//     //$this->intensity = 2;
//     $this->worker(function(){
//         echo "work1";
//         yield $this->delay(1); 
//         echo "E1";
//     });    
//     $this->worker(function(){
//         echo "work2";
//         yield $this->delay(2); 
//         echo "e2";
//     });
// });

// Sup::start(function(){ 
//     //长运行（单独进程创建
//     $this->service(function(){
//         //匿名函数无类型默认spawn 
//         $server = Socket::listen('tcp://0.0.0.0:8080');
//         while(1){ 
//             $client = yield Socket::accept($server);
//             co(function()use($client){
//                 $data = yield Socket::recv($client);
//             });
//         }
//     });
//     $this->service(function(){ 
//         $conn = new \Connection\TcpListenConnection(self::$reg_addr);
//         $conn->onMessage=\Hprose\Future\wrap(function($client,$addr){
//             $coid = yield Actor::spawn(function(){
                
//             });
//             $conn = new Workerman\Connection\TcpConnection($client);
//             $conn->onMessage=function($buff)use($coid){
//                 Actor::send($coid,$buff);
//             };            
//         }); 
//     });
//     //短运行（进程池创建
//     $this->worker(function(){
//         //匿名函数无类型默认spawn

//         yield $this->delay(2);
//         echo "start2\n";
//     });
// });
 

//print_r(Booru::$client_data) 
// class A extends Actor{function AAAA(){
//     yield $this->delay(1); 
//     $this->trapexit(true);
  
//     echo "link:".yield $this->spawn_link(function(){
//         yield $this->receive();
//         yield $this->delay(1);
//     }); 
//     echo "\n";  
//     echo "link:".yield $this->spawn_link(function(){
//         yield $this->receive();
//         yield $this->delay(1);
//     }); 
//     echo "\n";  
//     echo "link:".yield $this->spawn_link(function(){
//         yield $this->receive();
//         yield $this->delay(1);
//     }); 
//     echo "\n";   

//     yield $this->delay(1); 
//     print_r(new Booru);
//     echo "wait\n";
//     yield $this->receive();
//     yield $this->receive();
//     yield $this->receive();
//     echo "exit\n";
//     // yield $this->delay(1); 
//     // print_r(new Booru);
// }}
// Actor::spawn([A::class,'AAAA']);

 
// A::start();  
//Actor::spawn_opt(function(){  
    // $this->trapexit(true);
    // while(1){ 
    //     for ($i=0; $i < 1000; $i++) {   
    //         $a = [
    //             yield static::spawn_link([A::class,'AA']),
    //             yield static::spawn_link([A::class,'BB']), 
    //             yield static::spawn_link([A::class,'BB']), 
    //             yield static::spawn_link([A::class,'BB']), 
    //             yield static::spawn_link([A::class,'BB']), 
    //             yield static::spawn_link([A::class,'BB']), 
    //         ];     
    //         $message = yield $this->receive();   
    //         echo "------RESTART-------\n";
    //         foreach ($a as $coid) { 
    //               $this->unlink($coid);
    //               $this->exit($coid,'restart');
    //         }  
    //         yield static::delay(0.01);
    //         echo "\n\n\n\n\n\n"; 
    //     }
    //     //yield static::delay(4);// Cannot resume an already running generato
    // }
    // return;
    // echo "ROOT:".$this->coid;
    // echo "\n"; 
//});

// Actor::spawn_opt(function(){ 
//     yield \Proc::delay(2);
//     $this->trapexit(true); 
  
//     while(1){ 
//         yield static::spawn_link(function(){
//             yield static::spawn_link(function(){
//                 yield static::spawn_link(function(){
//                     // echo "BB";
//                     // yield \Proc::delay(0.1);
//                     echo "END\n";
//                 }); 
//                 yield static::receive();
//             });
//             yield static::receive();
//         });
        
//         //yield \Proc::delay(1);
//         $res = yield $this->receive();
//         // echo "EXIT\n"; 
//         //yield \Proc::delay(1);
//     } 
// }); 


// \lib\BaseActor::spawn_opt(function(){  
//     //$this->trapexit(true); 
//     $coid = yield $this->spawn_link(function(){  //1
//         yield $this->spawn_link(function(){ //2
//             yield $this->spawn_link(function(){ //3
//                 yield $this->spawn_link(function(){ //4   
//                     //yield $this->receive(); 
//                     yield \Proc::delay(1);     
//                 });   
//                 yield \Proc::delay(999);   
//                 //yield $this->receive();   
//             });   
//             yield $this->receive();   
//         });   
//         yield \Proc::delay(999);   
//         //yield $this->receive();   
//     });  
//     yield \Proc::delay(9999);  
// });
 

//exit("EEEEEEEE???事件循环也一起终止????还有这种操作?????EEEEEEEEEE");



 
